import javax.swing.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Doudizhu extends JFrame {
    private List<Card> list = new ArrayList<>();
    private List<Card> one = new ArrayList<>();
    private List<Card> two = new ArrayList<>();
    private List<Card> three = new ArrayList<>();
    private List<Card> bottom = new ArrayList<>();

    public Doudizhu() {

        this.init();
        this.xipai();
        this.fapai();
        //设置空布局
        this.setLayout(null);
        //创建组件
        for (int i = 0; i < 17; i++) {
            Icon icon1 = Util.getIcon(one.get(i).getName());
            JLabel label = new JLabel(icon1);
            label.setBounds(10, 550 - (30 * i), 105, 150);
            this.add(label);
        }
        for (int i = 0; i < 17; i++) {
            Icon icon1 = Util.getIcon(two.get(i).getName());
            JLabel label = new JLabel(icon1);
            label.setBounds(650 - (30 * i), 600, 105, 150);
            this.add(label);
        }
        for (int i = 0; i < 17; i++) {
            Icon icon1 = Util.getIcon(three.get(i).getName());
            JLabel label = new JLabel(icon1);
            label.setBounds(800, 550 - (30 * i), 105, 150);
            this.add(label);
        }
        for (int i = 0; i < 3; i++) {
            Icon icon1 = Util.getIcon(bottom.get(i).getName());
            JLabel label = new JLabel(icon1);
            label.setBounds(450-(30*i), 10, 105, 150);
            this.add(label);
        }
        JButton bnt = new JButton("开始");
        bnt.setBounds(420, 300, 100, 50);
        this.add(bnt);
        //设置窗口大小
        this.setSize(935, 800);
        //设置窗体的标题文字
        this.setTitle("斗地主");
        //设置窗体居中
        this.setLocationRelativeTo(null);
        //设置窗口关闭时程序结束
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        //设置窗体可见
        this.setVisible(true);

    }

    //初始化54张牌
    public void init() {
        String[] points = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        String[] flowers = {"♠", "♥", "♣", "♦"};
        for (int i = 0; i < points.length; i++) {
            for (int j = 0; j < flowers.length; j++) {
                //将带花色的扑克牌加入集合
                list.add(new Card(points[i], flowers[j], i, j, "/images/" + (i + 3) + "_" + (j + 1) + ".jpg"));
            }
        }
        list.add(new Card("小王", null, 50, 0, "/images/16_1.jpg"));
        list.add(new Card("大王", null, 99, 0, "/images/17_1.jpg"));
    }

    // 洗牌
    public void xipai() {
        Collections.shuffle(list);

    }

    // 发牌
    public void fapai() {
        one = list.subList(0, 17);
        two = list.subList(17, 34);
        three = list.subList(34, 51);
        bottom = list.subList(51, 54);
        sort(one);
        sort(two);
        sort(three);
    }

    //排序
    public void sort(List list) {
        list.sort(new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                if (o1.getValue() == o2.getValue()) {
                    return o1.getTypeValue() - o2.getTypeValue();
                } else {
                    return o1.getValue() - o2.getValue();
                }
            }
        });
    }

    public static void main(String[] args) {
        new Doudizhu();

    }
}

